﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace SVAP
{
    public partial class MainForm : Form
    {
        ////internal string filePath = "";

        //internal ManageAlgorithmForm maf = null;
        //internal RecentPictureForm rpf = null;
        //internal RecentVideoForm rvf = null;
        //internal ResultConfigForm rcf = null;

        //public MainForm()
        //{
        //    InitializeComponent();
        //}

        ////显示最近打开的视频文件
        //private void showRecentVideoToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    if (rvf == null || rvf.IsDisposed)
        //    {
        //        rvf = new RecentVideoForm();
        //    }

        //    rvf.Owner = this;
        //    rvf.Show();
        //    rvf.Focus();

        //}

        ////显示最近打开的图片文件
        //private void showRecentPicToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    if (rpf == null || rpf.IsDisposed)
        //    {
        //        rpf = new RecentPictureForm();
        //    }

        //    rpf.Owner = this;
        //    rpf.Show();
        //    rpf.Focus();
        //}

        ////录制视频
        //private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    MessageBox.Show("hi");
        //}

        ////关于
        //private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    AboutBox_Product abp = new AboutBox_Product();

        //    if (abp.ShowDialog() == DialogResult.OK)
        //        abp.Hide();
        //}

        ////打开本地图片文件
        //private void openPictureToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        OpenFileDialog openPicture = new OpenFileDialog();
        //        openPicture.FileName = "";
        //        openPicture.Filter = "jpg图片(*.jpg)|*.jpg|所有文件(*.*)|*.*";

        //        if (openPicture.ShowDialog() == DialogResult.OK)
        //        {
        //            pictureMDI pmdi = new pictureMDI();
        //            pmdi.MdiParent = this;

        //            pmdi.filePath = openPicture.FileName;  //获取选定的图片文件的路径

        //            //在缩微窗口显示图片
        //            Bitmap selectedPicture = new Bitmap(openPicture.FileName);
        //            pmdi.pictureBox_picMDI.Image = (Image)selectedPicture;

        //            //显示图片的属性信息
        //            FileInfo fi = new FileInfo(openPicture.FileName);  //文件的大小
        //            pmdi.lbl_picMDI_att.Text = "名称: " + Path.GetFileName(openPicture.FileName) + "\r\n" + "\r\n" + "大小: " + fi.Length.ToString() + "字节\r\n" + "\r\n" + "尺寸: " + selectedPicture.Size.Height.ToString() + " x " + selectedPicture.Size.Width.ToString() + "\r\n" + "\r\n" + "修改日期: " + fi.LastWriteTime.ToString();

        //            pmdi.WindowState = FormWindowState.Maximized;
        //            pmdi.Show();
        //        }
        //    }
        //    catch (Exception openPictureError)
        //    {
        //        MessageBox.Show("打开指定文件失败！ " + openPictureError.Message.ToString());
        //    }
        //}

        ////打开本地视频文件
        //private void openVideoToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        OpenFileDialog openVideo = new OpenFileDialog();
        //        openVideo.FileName = "";
        //        openVideo.Filter = "avi格式(*.avi)|*.avi|所有文件(*.*)|*.*";

        //        if (openVideo.ShowDialog() == DialogResult.OK)
        //        {
        //            VideoForm vf = new VideoForm();
        //            //vf.Owner = this;
        //            vf.MdiParent = this;
        //            vf.WindowState = FormWindowState.Maximized;
        //            vf.axWindowsMediaPlayer_main.URL = openVideo.FileName;
        //            //vf.axWindowsMediaPlayer_main.settings.autoStart = true;   //不自动播放
        //            vf.label_main.Text = "名称: " + vf.axWindowsMediaPlayer_main.currentMedia.name + "\r\n" + "\r\n" + "类型: " + vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileType") + "\r\n" + "\r\n" + "大小: " + (long.Parse(vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileSize")) / 1024 / 1024).ToString() + "MB\r\n" + "\r\n" + "持续时间: "; //+ vf.axWindowsMediaPlayer_main.currentMedia.durationString;
        //            vf.Show();                   
        //        }
        //    }
        //    catch (Exception openVideoError)
        //    {
        //        MessageBox.Show("打开指定文件失败！ " + openVideoError.Message.ToString());
        //    }
        //}

        ////算法管理
        //private void AlgorithmManagerToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    //ManageAlgorithmForm maf = new ManageAlgorithmForm();
        //    //maf.Owner = this;
        //    //maf.Show();

        //    if (maf == null || maf.IsDisposed)
        //    {
        //        maf = new ManageAlgorithmForm();
        //    }

        //    maf.Owner = this;
        //    maf.Show();
        //    maf.Focus();
        //}

        ////统计结果配置
        //private void ResultConfigToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    if (rcf == null || rcf.IsDisposed)
        //    {
        //        rcf = new ResultConfigForm();
        //    }

        //    rcf.Owner = this;
        //    rcf.Show();
        //    rcf.Focus();
        //}

        ////使用手册
        //private void HandBookToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    //测试子窗体用，无他用
        //    pictureMDI pmdi = new pictureMDI();
        //    pmdi.MdiParent = this;
        //    pmdi.WindowState = FormWindowState.Maximized;
        //    pmdi.Show();
        //}

        ////规范标准
        //private void StandardToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    //测试子窗体用，无他用
        //    //VideoMDI vmdi = new VideoMDI();
        //    //vmdi.MdiParent = this;
        //    //vmdi.WindowState = FormWindowState.Maximized;
        //    //vmdi.Show();

        //    VideoForm vf = new VideoForm();
        //    vf.MdiParent = this;
        //    vf.WindowState = FormWindowState.Maximized;
        //    vf.Show();
        //}

        ////退出
        //private void ExitToolStripMenuItem1_Click(object sender, EventArgs e)
        //{
        //    Application.Exit();
        //}

        ////快速获取视频
        //private void screenCutToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    Image img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
        //    Graphics g = Graphics.FromImage(img);
        //    g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
        //    IntPtr dc = g.GetHdc();
        //    g.ReleaseHdc(dc);
        //    ScreenBody body = new ScreenBody();
        //    body.BackgroundImage = img;
        //    //body.Parent = this;
        //    body.Show();
        //}


        //internal string filePath = "";

        internal ManageAlgorithmForm maf = null;
        internal RecentPictureForm rpf = null;
        internal RecentVideoForm rvf = null;
        internal ResultConfigForm rcf = null;
        internal GetVideoForm gvf = null;
        internal AboutBox_Product abp = null;
        internal ConverterForm ctf = null;
        internal SpecificationForm sf = null;

        public static string strPictureURL;

        public enum KeyModifiers  //组合键枚举
        {
            None = 0,
            Alt = 1,
            Control = 2,
            Shift = 4,
            Windows = 8
        }

        [System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, Keys vk);

        [System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        public MainForm()
        {
            InitializeComponent();
        }

        //按下设定的键时调用该函数
        private void ProcessHotkey(Message m)
        {
            IntPtr id = m.WParam; //IntPtr用于表示指针或句柄的平台特定类型
            string sid = id.ToString();
            switch (sid)
            {
                case "100":
                    //MessageBox.Show("A");

                    Image img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
                    Graphics g = Graphics.FromImage(img);
                    g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
                    IntPtr dc = g.GetHdc();
                    g.ReleaseHdc(dc);
                    ScreenBody body = new ScreenBody();
                    body.BackgroundImage = img;
                    //body.Parent = this;
                    body.Show();

                    break;
                //case "200":
                //    MessageBox.Show("B");
                //    break;
            }
        }

        //加载系统热键
        private void MainForm_Load(object sender, EventArgs e)
        {
            //RegisterHotKey(Handle, 100, KeyModifiers.Alt | KeyModifiers.Control, Keys.Q); //这时热键为Alt+CTRL+Q
            RegisterHotKey(Handle, 100, 2, Keys.Down); //这时热键为CTRL+Down
        }

        //重写WndProc()方法，通过监视系统消息，来调用过程
        protected override void WndProc(ref Message m)//监视Windows消息
        {
            try
            {
                const int WM_HOTKEY = 0x0312;//如果m.Msg的值为0x0312那么表示用户按下了热键
                switch (m.Msg)
                {
                    case WM_HOTKEY:
                        ProcessHotkey(m);//按下热键时调用ProcessHotkey()函数
                        break;
                }
                base.WndProc(ref m); //将系统消息传递自父类的WndProc
            }
            catch
            { }
        }

        //显示最近打开的视频文件
        private void showRecentVideoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (rvf == null || rvf.IsDisposed)
            {
                rvf = new RecentVideoForm();
            }

            rvf.Owner = this;
            rvf.Show();
            rvf.Focus();

        }

        //显示最近打开的图片文件
        private void showRecentPicToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (rpf == null || rpf.IsDisposed)
            {
                rpf = new RecentPictureForm();
            }

            rpf.Owner = this;
            rpf.Show();
            rpf.Focus();
        }

        //录制视频
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (gvf == null || gvf.IsDisposed)
            {
                gvf = new GetVideoForm();
            }

            gvf.Owner = this;
            gvf.Show();
            gvf.Focus();
        }

        //关于
        private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (abp == null || abp.IsDisposed)
            {
                abp = new AboutBox_Product();
            }

            if (abp.ShowDialog() == DialogResult.OK)
                abp.Hide();
        }

        //打开本地图片文件
        private void openPictureToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openPicture = new OpenFileDialog();
                openPicture.FileName = "";
                //openPicture.Filter = "bmp图片(*.bmp)|*.bmp|所有文件(*.*)|*.*";
                openPicture.Filter = "bmp图片(*.bmp)|*.bmp|jpg图片(*.jpg)|*.jpg";

                if (openPicture.ShowDialog() == DialogResult.OK)
                {
                    //修改频率
                    ModifyFre mf = new ModifyFre();
                    mf.ModifyFrePic(openPicture.FileName);

                    pictureMDI pmdi = new pictureMDI();
                    pmdi.MdiParent = this;

                    pmdi.filePath = openPicture.FileName;  //获取选定的图片文件的路径

                    //在缩微窗口显示图片
                    Bitmap selectedPicture = new Bitmap(openPicture.FileName);
                    pmdi.pictureBox_picMDI.Image = (Image)selectedPicture;

                    //显示图片的属性信息
                    FileInfo fi = new FileInfo(openPicture.FileName);  //文件的大小
                    pmdi.lbl_picMDI_att.Text = "名称: " + Path.GetFileName(openPicture.FileName) + "\r\n" + "\r\n" + "大小: " + fi.Length.ToString() + "字节\r\n" + "\r\n" + "尺寸: " + selectedPicture.Size.Height.ToString() + " x " + selectedPicture.Size.Width.ToString() + "\r\n" + "\r\n" + "修改日期: " + fi.LastWriteTime.ToString();

                    pmdi.WindowState = FormWindowState.Maximized;
                    pmdi.Show();
                }
                strPictureURL = openPicture.FileName;// added by Zheng Xiao 2010-4-26
                //MessageBox.Show(strPictureURL);
            }
            catch
            {
                MessageBox.Show("打开指定文件失败!");
            }
        }

        //打开本地视频文件
        private void openVideoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openVideo = new OpenFileDialog();
                openVideo.FileName = "";
                openVideo.Filter = "avi格式(*.avi)|*.avi|所有文件(*.*)|*.*";

                if (openVideo.ShowDialog() == DialogResult.OK)
                {
                    //修改频率
                    ModifyFre mf = new ModifyFre();
                    mf.ModifyFreVideo(openVideo.FileName);

                    VideoForm vf = new VideoForm();
                    //vf.Owner = this;
                    vf.MdiParent = this;
                    vf.WindowState = FormWindowState.Maximized;
                    vf.axWindowsMediaPlayer_main.URL = openVideo.FileName;
                    //vf.axWindowsMediaPlayer_main.settings.autoStart = true;   //不自动播放
                    vf.label_main.Text = "名称: " + vf.axWindowsMediaPlayer_main.currentMedia.name + "\r\n" + "\r\n" + "类型: " + vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileType") + "\r\n" + "\r\n" + "大小: " + (long.Parse(vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileSize")) / 1024 / 1024).ToString() + "MB\r\n" + "\r\n" + "持续时间: "; //+ vf.axWindowsMediaPlayer_main.currentMedia.durationString;
                    vf.Show();
                }
            }
            catch
            {
                MessageBox.Show("打开指定文件失败!");
            }
        }

        //算法管理
        private void AlgorithmManagerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //ManageAlgorithmForm maf = new ManageAlgorithmForm();
            //maf.Owner = this;
            //maf.Show();

            if (maf == null || maf.IsDisposed)
            {
                maf = new ManageAlgorithmForm();
            }

            maf.Owner = this;
            maf.Show();
            maf.Focus();
        }

        //统计结果配置
        private void ResultConfigToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (rcf == null || rcf.IsDisposed)
            {
                rcf = new ResultConfigForm();
            }

            rcf.Owner = this;
            rcf.Show();
            rcf.Focus();
        }

        //使用手册
        private void HandBookToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //测试子窗体用，无他用
            //pictureMDI pmdi = new pictureMDI();
            //pmdi.MdiParent = this;
            //pmdi.WindowState = FormWindowState.Maximized;
            //pmdi.Show();
        }

        //规范标准
        private void StandardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (sf==null||sf.IsDisposed)
            {
                sf = new SpecificationForm();
            }
            sf.Owner = this;
            sf.Show();
            sf.Focus();
        }

        //从菜单退出
        private void ExitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            UnregisterHotKey(Handle, 100);//卸载第1个快捷键
            Application.Exit();
        }

        //快速截图
        private void screenCutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Image img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
            Graphics g = Graphics.FromImage(img);
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
            IntPtr dc = g.GetHdc();
            g.ReleaseHdc(dc);
            ScreenBody body = new ScreenBody();
            body.BackgroundImage = img;
            //body.Parent = this;
            body.Show();
            body.Focus();
        }

        //单击叉退出程序
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            UnregisterHotKey(Handle, 100);//卸载第1个快捷键
        }

        private void 视频转换ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ctf == null || ctf.IsDisposed)
            {
                ctf = new ConverterForm();
            }

            ctf.Owner = this;
            ctf.Show();
            ctf.Focus();
        }
    }
}
